package services

import (
	"errors"
	"github.com/jinzhu/gorm"
	"gowww/comeon/models"
)

type MenuList struct {
	Name        string `json:"name"`
	CurrentPage int    `json:"currentPage"`
	PageSize    int    `json:"pageSize"`
}

func (list *MenuList) List() (data interface{}) {
	var menuModel = &models.MenuIncludeChildren{
		Name: list.Name,
	}
	count, ret, err := menuModel.GetMenuList(list.CurrentPage, list.PageSize)
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {

	}
	arr := make(map[string]interface{})
	arr["count"] = count
	arr["data"] = ret
	return arr
}

type MenuAdd struct {
	Name     string `json:"name"`
	ParentId uint64 `json:"parentId"`
	Status   int    `json:"status"`
	Layout   string `json:"layout"`
	Path     string `json:"path"`
}

//
//  Add
//  @Description: 角色添加
//  @receiver a
//  @return error
//
func (a *MenuAdd) Add() error {

	var menuModel = &models.Menu{
		Name:     a.Name,
		ParentId: a.ParentId,
		Status:   a.Status,
		Layout:   a.Layout,
		Path:     a.Path,
	}

	if a.Name == "" {
		return errors.New("菜单名称不能为空")
	}

	if a.Layout == "" {
		return errors.New("Layout不能为空")
	}

	if a.Path == "" {
		return errors.New("Path不能为空")
	}
	id := menuModel.Add()
	if id <= 0 {
		return errors.New("添加失败")
	}
	return nil
}
